<?php
/**
 * Generates the whole tree of instances given a classname
 */
class IOC_TreeBinder implements IOC_Binder {
    /**
     * Gets an instance of the classname
     * @param $className
     * @return object
     */
    public function get($className) {
        $builder = new ReflectionClass($className);
        //gets the constructor
        $constructor = $builder->getConstructor();
        if(!is_null($constructor)) {
            //checks all the parameters for the constructor
            $params = $constructor->getParameters();
            $args = array();
            foreach($params as $param) {
                $clazz = $param->getClass();
                if(!$clazz) {
                    throw new IOC_BindingException("Inversion of Control injector cannot find class " .
				               "for $param in $constructor.<br/>\n" .
				               "<b>Have you added a binding for the class?<b>");
                }
                $args[]=IOC_Injector::get()->getInstance($param->getClass()->getName());
            }
            return $builder->newInstanceArgs($args);
        } else {
            return $builder->newInstance();
        }
    }
}
?>